'use strict';

const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, DECIMAL, TEXT, DATE, ENUM } = app.Sequelize;

  const IdleTransfer = app.model.define('idle_transfer', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    item_id: { type: INTEGER, allowNull: false, comment: '物资/设备ID' },
    category_id: { type: INTEGER },
    category_name: { type: STRING },
    address: { type: STRING(255) },
    province: { type: STRING(255) },
    city: { type: STRING(255) },
    type: { type: STRING },
    quantity: { type: INTEGER, defaultValue: 0, comment: '调拨数量' },
    transfer_price: {
      type: STRING,
    },
    status: { type: ENUM('pending', 'approved', 'rejected', 'completed', 'canceled'), defaultValue: 'pending', comment: '状态：待审批、已同意、已拒绝、已完成、已取消' },
    apply_time: { type: DATE, defaultValue: app.Sequelize.fn('NOW'), comment: '申请时间' },
    approve_time: { type: DATE, comment: '审批时间' },
    start_date: {
      type: DATE, comment: '开始日期',
      get() {
        return dayjs(this.getDataValue('start_date')).format('YYYY-MM-DD');
      }
    },
    end_date: {
      type: DATE, comment: '结束日期',
      get() {
        return dayjs(this.getDataValue('end_date')).format('YYYY-MM-DD');
      }
    },
    complete_time: {
      type: DATE, comment: '完成时间',
      get() {
        return dayjs(this.getDataValue('complete_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    remark: { type: TEXT, comment: '备注' },
    apply_user_id: { type: INTEGER, allowNull: false, comment: '申请人ID' },
    apply_user_name: { type: STRING(50), comment: '申请人姓名' },
    create_user_id: { type: INTEGER, comment: '创建人ID' },
    create_user_name: { type: STRING(50), comment: '创建人姓名' },
    approve_user_id: { type: INTEGER, comment: '审批人ID' },
    approve_user_name: { type: STRING(50), comment: '审批人姓名' },
    approved_user_id: { type: INTEGER, comment: '实际审批人ID' },
    approved_user_name: { type: STRING(50), comment: '实际审批人姓名' },
    approve_remark: { type: STRING(255), comment: '审批人备注' },
    create_time: { type: DATE, defaultValue: app.Sequelize.fn('NOW') },
    update_time: { type: DATE, defaultValue: app.Sequelize.fn('NOW') }
  }, {
    tableName: 'idle_transfer',
    timestamps: false,
  });

  // 定义关联关系
  IdleTransfer.associate = function() {
    app.model.IdleTransfer.belongsTo(app.model.IdleItem, { foreignKey: 'item_id' });
  };

  return IdleTransfer;
};
